<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Oauth extends CI_Controller{

    function __construct() {
        parent::__construct();
    }

    public function index() {
        if (isset($_SESSION['token'])) {
            Header("Location: /interest/index"); 
			return true;
		}
        /**/
        $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY, null, null);
        $code_url = $o->getAuthorizeURL(WB_CALLBACK_URL);
        $this->load->view("oauth/index_view", array("code_url" => $code_url));
    }

    public function authorize() {
        $o = new SaeTOAuthV2(WB_AKEY, WB_SKEY);
        if (isset($_REQUEST['code'])) {
            $keys = array();
            $keys['code'] = $_REQUEST['code'];
            $keys['redirect_uri'] = WB_CALLBACK_URL;
            try {
                $token = $o->getAccessToken('code', $keys);
            } catch (OAuthException $e) {
                
            }
        }

        if (isset($token)) {
            $_SESSION['token'] = $token;
            setcookie('weibojs_' . $o->client_id, http_build_query($token));
            Header("Location: /interest/index"); 
        } else {
            $this->load->view("show/error_view");
        }
    }

    public function score() {
        $score = array();
        if (isset($_GET['uid'])) {
            $uid = $_GET['uid'];
            $this->load->model('score_model');
            $scoredb = new Score_model();
            $score = $scoredb->getScore($uid);
        }
        $this->load->view("score/score_view", array("score" => $score));
    }

}

?>